<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>

<script>
    var a = 1234567894532;
    var b = 673439.4542;

    function numFormat(num) {
        num = num.toString().split('.'); // 分割出来是一个数组
        var arr = num[0].split("").reverse(); // 转换成字符数组并倒序排列
        // var arr = num[0].split("").reverse();
        var res = [];
        let len = arr.length;
        for (let i = 0; i < len; i++) {
            if (i != 0 && i % 3 == 0) {
                res.push(",");
            }
            res.push(arr[i]);
        }
        res.reverse(); // 倒序为正确的顺序
        if (num[1]) { // 有小数添加小数部分
            res = res.join("").concat("." + num[1]);
        } else {
            res = res.join("");
        }

        return res;
    }

    console.log(numFormat(a)); 
    console.log(numFormat(b));


</script>


</html>